home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / dev / src / expat-src.lha / expat-1.95.2 / examples / elements.c next >
Encoding:
C/C++ Source or Header  |  2001-12-29  |  1.6 KB  |  79 lines

  1. /* This is simple demonstration of how to use expat. This program
  2. reads an XML document from standard input and writes a line with the
  3. name of each element to standard output indenting child elements by
  4. one tab stop more than their parent element. */
  5.  
  6. #include <stdio.h>
  7. #ifndef AMIGA
  8. #include <expat.h>
  9. #else
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <expat/expat.h>
  13. #include <proto/exec.h>
  14. #include <proto/expat.h>
  15.  
  16. struct Library *ExpatBase = NULL;
  17.  
  18. #endif
  19.  
  20. static void
  21. startElement(void *userData, const char *name, const char **atts)
  22. {
  23.   int i;
  24.   int *depthPtr = userData;
  25.   for (i = 0; i < *depthPtr; i++)
  26.     putchar('\t');
  27.   puts(name);
  28.   *depthPtr += 1;
  29. }
  30.  
  31. static void
  32. endElement(void *userData, const char *name)
  33. {
  34.   int *depthPtr = userData;
  35.   *depthPtr -= 1;
  36. }
  37.  
  38. int
  39. main(int argc, char *argv[])
  40. {
  41.   char buf[BUFSIZ];
  42.   XML_Parser parser;
  43.   int done;
  44.   int depth = 0;
  45.  
  46. #ifdef AMIGA
  47.   ExpatBase = (APTR) OpenLibrary("expat.library", 0);
  48.   if(!ExpatBase)
  49.   {
  50.     puts("\nCouldn't open expat.library\n");
  51.     exit(20);
  52.   }
  53. #endif
  54.  
  55.   parser = XML_ParserCreate(NULL);
  56.   
  57.   XML_SetUserData(parser, &depth);
  58.   XML_SetElementHandler(parser, startElement, endElement);
  59.   do {
  60.     size_t len = fread(buf, 1, sizeof(buf), stdin);
  61.     done = len < sizeof(buf);
  62.     if (!XML_Parse(parser, buf, len, done)) {
  63.       fprintf(stderr,
  64.           "%s at line %d\n",
  65.           XML_ErrorString(XML_GetErrorCode(parser)),
  66.           XML_GetCurrentLineNumber(parser));
  67. #ifdef AMIGA  
  68.       CloseLibrary((APTR) ExpatBase);;
  69. #endif
  70.       return 1;
  71.     }
  72.   } while (!done);
  73.   XML_ParserFree(parser);
  74. #ifdef AMIGA  
  75.       CloseLibrary((APTR) ExpatBase);;
  76. #endif
  77.   return 0;
  78. }
  79.